﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace CERTCOP_C
{
    public class Session
    {
        private int _sessionId;
        private List<Object> _sessionReceivedMessages;
        private Object _lockingObject;

        public Session(int sessionId)
        {
            _sessionId = sessionId;
            _sessionReceivedMessages = new List<object>();
            _lockingObject = new Object();
        }

        public void addSessionMessage(Object obj)
        {
            lock (_lockingObject)
            {
                Monitor.Enter(obj);
                _sessionReceivedMessages.Add(obj);
                Monitor.Pulse(obj);
            }
        }

        public Object getSessionMessage()
        {
            lock (_lockingObject)
            {
                Monitor.Enter(_lockingObject);
                if (_sessionReceivedMessages.Count == 0)
                {
                    Monitor.Wait(_lockingObject);                    
                }

                Object obj = _sessionReceivedMessages[0];
                _sessionReceivedMessages.Remove(obj);
                return obj;
            }
        }
    }
}
